package com.mastercard.assets.web.struts.actions;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

import com.mastercard.assets.jpa.TblAsset;
import com.mastercard.assets.jpa.TblCampania;
import com.mastercard.assets.jpa.TblCategoria;
import com.mastercard.assets.jpa.TblCuentaPaquete;
import com.mastercard.assets.jpa.TblPaquete;
import com.mastercard.assets.jpa.TblSubcateg;
import com.mastercard.assets.jpa.TblTipoCampania;
import com.mastercard.assets.jpa.TblTipoCuenta;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.AssetAltaForm;
import com.mastercard.assets.web.struts.forms.AssetAsignarPaquetesForm;
import com.mastercard.assets.web.struts.forms.AssetBorrarForm;
import com.mastercard.assets.web.struts.forms.AssetBuscarForm;
import com.mastercard.assets.web.struts.forms.AssetEditarForm;
import com.mastercard.assets.web.struts.forms.AssetModificarPaquetesForm;
import com.mastercard.assets.web.struts.forms.CampanasPorAutorizarForm;
import com.mastercard.assets.web.struts.forms.GenerarCampanaForm;
import com.mastercard.assets.web.struts.forms.GenerarCampanaRegistrarForm;
import com.mastercard.assets.web.struts.forms.ListarCampanasForm;
import com.mastercard.assets.web.struts.forms.LoginForm;
import com.mastercard.assets.web.util.to.ManejadorPaquetesAsignados;
import com.mastercard.assets.web.util.to.PaqueteAutorizado;
import com.mastercard.assets.web.util.to.UserLogin;

public class ListarCampanasCancelar
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		// verifica que el rol en el que esta es banco (2)
		int role = user.getActualRoleId( ) ;
		
		if( role != 1 )
			return mapping.findForward( "RoleNoValido" ) ;
		
		// formulario
		ListarCampanasForm f = ( ListarCampanasForm ) form ;
		
		// en caso de null
		if( f == null )
			return mapping.findForward( "SourceInvalid" ) ;
		
		int operation = f.getOperation( ) ;
		int cveCampania = f.getCveCampania( ) ;
		int page = f.getPage( ) ;
		
		// verifica la pagina de la que viene
		// en caso de no venir de alguna
		if( operation < 1 )
		{
			user.setNextWorkArea( "/WEB-INF/pages/WorkArea/ListarCampanasSourceInvalid.jsp" ) ;
			return mapping.findForward( "SourceInvalid" ) ;
		}
		// en caso de que quiera regresar
		else if( operation == 1 )
		{
			user.setNextWorkArea( "/ListarCampanasIn.do?cveCampania=" + cveCampania ) ;
			return mapping.findForward( "success" ) ;
		}
		// en caos de que quiera borrar
		else if( operation == 2 )
		{
			TblCampania campania = MasterCardDAO.findCampaignById( cveCampania ) ;
			
			try
			{
				MasterCardDAO.cancelCampaign( cveCampania , user.getCveUsuario( ), 2 ) ;
				
				request.setAttribute( "Campania" , campania ) ;
				user.setNextWorkArea( "/ListarCampanasIn.do" ) ;
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "success" ) ;
			}catch( Exception e )
			{
				user.setMessageError( e.getMessage( ) ) ;
				user.setNextWorkArea( "/WEB-INF/pages/WorkArea/ListarCampanasError.jsp" ) ;
				MasterCardDAO.closeEntityManager( ) ;
                                return mapping.findForward( "success" ) ;
			}
		}
		
                MasterCardDAO.closeEntityManager( ) ;
		user.setNextWorkArea( "/WEB-INF/pages/WorkArea/ListarCampanasSourceInvalid.jsp" ) ;
		return mapping.findForward( "SourceInvalid" ) ;
		
	}
	
}
